/*
 * Created on Mar 29, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.ba360.struts.action;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.ba360.portal.database.DBConnectionImpl;
import com.ba360.struts.form.BlogForm;

/**
 * @author ROnat
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class BlogAction extends Action {

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse res) throws Exception {
		String success="";
		String action = req.getParameter("action");
		HttpSession session = req.getSession();
		Connection conn = null;
		Statement stmt = null;
		DBConnectionImpl dbConn = new DBConnectionImpl();
		conn = dbConn.getConnection();
		
		BlogForm bForm = (BlogForm)form;
		
		if (action==null)
		{
			stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery("select * from blog");
			ArrayList al= new ArrayList();
			while (rs.next())
			{
				BlogForm bf = new BlogForm();
				bf.setBlogid(rs.getString("blogid"));
				bf.setContent(rs.getString("Content"));
				bf.setDateCreated(rs.getTimestamp("date_created"));
				bf.setDateModified(rs.getTimestamp("date_modified"));
				bf.setSummary(rs.getString("Abstract"));
				bf.setTitle(rs.getString("Content"));
				Statement stmt_ = conn.createStatement();
				ResultSet rs_ = stmt_.executeQuery("select count(*) from reviews where blogid="+bf.getBlogid());
				rs_.next();
				String count = rs_.getString(0);
				rs_.close();
				stmt_.close();
				bf.setNoOfReviews(count);
				al.add(bf);
				
				
			}
			rs.close();
			stmt.close();
			session.setAttribute("AllBlogs",al);
			success = "bloghome";
		}
		else if (action!=null){
			if (action.equals("AddNewBlog"))
			{
				success="NewBlog";
			}
			if (action.equals("AddBlog"))
			{
				String maxId = "";
				ResultSet rs = stmt.executeQuery("select COALESCE(max(business_id),0)+1 from businessinfo");
				
				rs.next();
				maxId = rs.getString(1);
				rs.close();
				stmt = conn.createStatement();
				//String query = "insert into blog("
				//stmt.executeQuery();
				success = "bloghome";
			}
			if (action.equals("AddReview"))
			{
				success = "newReview";
			}
			if (action.equals("PostReview"))
			{
				success = "thisbloghome";
			}
			if (action.equals("ViewBlog"))
			{
				success = "thisbloghome";
			}
			if (action.equals("ViewReview"))
			{
				success = "thisblogreview";
			}
			if (action.equals("DeleteBlog"))
			{
				success = "myads";
			}
			if (action.equals("ModifyBlog"))
			{
				success = "myads";
			}
		}
		return mapping.findForward(success);
	}
}
